package com.brp.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: dy
 * @Date: 2022/3/16 - 03 - 16 - 10:28
 * @Description: com.dy.util
 * @version: 1.0
 */
@Configuration // 全局配置
@ComponentScan(basePackages = {"com.brp"})//开启注解扫描
@MapperScan(basePackages = {"com.brp.dao"}) // 开启mapper映射
@EnableAspectJAutoProxy(proxyTargetClass = true) //开启cglib代理
@EnableTransactionManagement //开启事务管理
public class ApplicationConfig {
    //配置dataSource
    @Bean
    public DataSource dataSource(DBProperties dbProperties){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(dbProperties.getDriverClassName());
        druidDataSource.setUrl(dbProperties.getUrl());
        druidDataSource.setUsername(dbProperties.getUsername());
        druidDataSource.setPassword(dbProperties.getPassword());
        druidDataSource.setInitialSize(3);
        druidDataSource.setMaxActive(10);
        druidDataSource.setMinIdle(3);
        return druidDataSource;
    }
    //配置SqlSessionFactoryBean,使用的是mp的类
    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,@Autowired MybatisPlusInterceptor mybatisPlusInterceptor) throws IOException {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        //设施mybatis数据源
        sqlSessionFactory.setDataSource(dataSource);
        //设置mybatis全局配置路径
        sqlSessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        //mapper文件的位置
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
        sqlSessionFactory.setTypeAliasesPackage("com.brp.po");
        sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
        return sqlSessionFactory;
    }
    //设置事务对象
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
        DataSourceTransactionManager tx = new DataSourceTransactionManager();
        tx.setDataSource(dataSource);
        return tx;
    }

    @Bean
    public PaginationInnerInterceptor getPaginationInnerInterceptor(){
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.valueOf("MYSQL"));
        return paginationInnerInterceptor;
    }

    @Bean
    public MybatisPlusInterceptor getMybatisPlusInterceptor(@Autowired PaginationInnerInterceptor paginationInnerInterceptor){
        List<PaginationInnerInterceptor> list = Arrays.asList(new PaginationInnerInterceptor[]{paginationInnerInterceptor});
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.setInterceptors(Collections.unmodifiableList(list));
        return mybatisPlusInterceptor;
    }
}
